package com.tgy.myredis.controller;

import cn.hutool.db.Session;

import com.tgy.myredis.config.WebSocketServer;
import com.tgy.myredis.service.CornService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/***
 * @ClassName: Controller
 * @Description:
 * @Auther: tanggy3
 * @Date: 10:56 2022/10/18
 * @version : V1.0
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class Controller {
    private final WebSocketServer assWebSocket;
    private final CornService scheduleTask;


    @RequestMapping("/aa")
    public String aa(String cron){
        log.info("new cron :{}", cron);
        scheduleTask.setTime(cron);
        return "ok";
    }


    @RequestMapping("/getCron")
    public String getCron(){
        return scheduleTask.getCron();
    }


    //页面请求
    @GetMapping("/index/{userId}")
    public ModelAndView socket(@PathVariable String userId) {
        ModelAndView mav = new ModelAndView("/socket1");
        mav.addObject("userId", userId);
        return mav;
    }

    //推送数据接口
    @ResponseBody
    @RequestMapping("/socket/push/{cid}/{message}")
    public Map pushToWeb(@PathVariable String cid, @PathVariable String message) {
        Map<String,Object> result = new HashMap<>();
        try {
            assWebSocket.sendInfo(message, cid);
            result.put("code", cid);
            result.put("msg", message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
